home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / userconf / perm.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  6KB  |  206 lines

  1. #include <string.h>
  2. #include <unistd.h>
  3. #include <pwd.h>
  4. #include <time.h>
  5. #include "../misc/misc.h"
  6. #include "../netconf/netconf.h"
  7. #include "userconf.h"
  8. #include "internal.h"
  9. #include "userconf.m"
  10.  
  11. static time_t lasttime = 0;
  12.  
  13. /*
  14.     Force a one minute "no question asked" root autorization for
  15.     one minute.
  16.  
  17.     Normally linuxconf does nothing priviledged unless the user
  18.     has provided some password (generally root). Some operation
  19.     are indeed priviledge but are accepted, for example, changing
  20.     his/her password.
  21. */
  22. void perm_forceok()
  23. {
  24.     lasttime = time(&lasttime);
  25. }
  26.  
  27. #define VALIDATION_TIME    (2*60)
  28.  
  29. /*
  30.     Get the crypt password of a user
  31. */
  32. static int perm_getupass (const char *username, char password[])
  33. {
  34.     /* #Specification: password / strategy / by hand
  35.         To support transparently standard and shadow password without
  36.         recompiling, linuxconf read manually the /etc/passwd and
  37.         /etc/shadow files.
  38.  
  39.         This is causing problem for NIS user though. This will
  40.         have to be fixed.
  41.     */
  42.     USERS users;
  43.     USER *usr = users.getitem(username);
  44.     int ret = -1;
  45.     if (usr != NULL){
  46.         SHADOW *shadow = users.getshadow(usr);
  47.         const char *pwd = usr->getpwd();
  48.         if (shadow != NULL){
  49.             pwd = shadow->getpwd();
  50.         }
  51.         strcpy (password,pwd);
  52.         ret = 0;
  53.     }
  54.     return ret;
  55. }
  56.  
  57. /*
  58.     Sent by the html mode to identify the user.
  59.     Currently, only root is accepted as a password.
  60.  
  61.     Ultimatly, www administrator could be defined.
  62. */
  63. void perm_setaccess (const char *username, const char *password)
  64. {
  65.     time_t curtime = time(&curtime);
  66.     lasttime = curtime - (VALIDATION_TIME + 1);    // Make last autorization
  67.                                                 // obsolete
  68.     if (strcmp(username,"root")==0){
  69.         if (simul_isdemo()){
  70.             /* #SpΘcification: linuxconf / demo mode / root password
  71.                 The root password in demo mode is always linux.
  72.             */
  73.             if (strcmp(password,"linux")==0){
  74.                 lasttime = curtime;
  75.             }
  76.         }else{
  77.             char upass[100];
  78.             if (perm_getupass(username,upass)!=-1){
  79.                 if (upass[0] != '\0'){
  80.                     if (strcmp(crypt(password, upass),upass)==0){
  81.                         lasttime = curtime;
  82.                     }
  83.                 }else if (password[0] == '\0'){
  84.                     lasttime = curtime;
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90. /*
  91.     Verify if there is password for root. If so ask the user
  92.     to enter it and validate it. So askrunlevel is "safe". No
  93.     one will be allowed to reconfigured the network if he don't
  94.     know the root password.
  95.  
  96.     Return != 0 if user if allowed to get in.
  97. */
  98. int perm_checkpass ()
  99. {
  100.     time_t curtime = time(&curtime);
  101.     /* #Specification: root access / timeout
  102.         When the user select a configuration task, the password for root
  103.         must be supplied. This "validation" is good for 2 minute.
  104.         It means that the user may do several configuration in one
  105.         minutes without being asked for the root password every time.
  106.         
  107.         If the user wait a minute or more, the password will be
  108.         asked again. Look safe and user friendly to me.
  109.     */
  110.     int ret = 1;
  111.     if (curtime - lasttime > VALIDATION_TIME){
  112.         char upass[100];
  113.         if (perm_getupass ("root",upass) == -1){
  114.             xconf_error (MSG_U(E_NOROOT
  115.                 ,"No root user in /etc/passwd\n"
  116.                  "Better to let you in"));
  117.         }else if (upass[0] != '\0'){
  118.             /* #Specification: root access / password validation
  119.                 When the admin/user must provide the root
  120.                 password, he has 3 chances.
  121.             */
  122.             if (dialog_mode == DIALOG_HTML){
  123.                 html_needpasswd();
  124.                 ret = 0;
  125.             }else{
  126.                 for (int i=0; i<3; i++){
  127.                     char passstr[MAX_LEN+1];
  128.                     if (xconf_inputpass (MSG_U(T_PASSREQ,"Password requiered")
  129.                         ,MSG_U(I_ENTERPASS
  130.                          ,"Enter password for root\n"
  131.                           "Only the superuser is allowed to perform\n"
  132.                           "configuration task.")
  133.                         ,help_nil
  134.                         ,passstr) != MENU_ACCEPT){
  135.                         ret = 0;
  136.                         break;
  137.                     }else if (strcmp(crypt(passstr, upass),upass)!=0){
  138.                         ret = 0;
  139.                         xconf_error (MSG_U(E_IVLDPASS,"Invalid password"));
  140.                     }else{
  141.                         ret = 1;
  142.                         break;
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.     }
  148.     if (ret) lasttime = curtime;
  149.     return ret;
  150. }
  151.  
  152. static int perm_html_mode = 0;
  153. /*
  154.     Check if the user is really root. If it is not, but the effective ID
  155.     is root, then ask for the root password.
  156.     Return != 0 if the real UID is root or the user knows the root password.
  157.  
  158.     It prints an informative message about the action which will occur.
  159. */
  160. int perm_rootaccess(const char *ctl, ...)
  161. {
  162.     /* #Specification: configurator / setuid root
  163.         The configurator (anything-conf) can be set setuid root.
  164.         It will allows normal users to get in and then will ask
  165.         for the root password at the proper time. This strategy
  166.         make the system friendlier. It allows normal user to
  167.         inspect the configuration (if allowed) and when finding
  168.         something odd, use the root password (if known) to fix
  169.         things, The idea here is that we generally think first
  170.         about getting somewhere and later about the permissions
  171.         needed to get there.
  172.  
  173.         The nice thing about this scheme is that this program
  174.         will deny root access automaticly after some time.
  175.  
  176.         If you don't like this, then don't set it setuid root. It
  177.         will operate correctly and won't bug you with password.
  178.     */
  179.     int ret = perm_html_mode ? 0 : getuid()==0;
  180.     if (geteuid()==0){
  181.         if (!ret){
  182.             ret = perm_checkpass();
  183.         }
  184.     }else{
  185.         va_list list;
  186.         va_start (list,ctl);
  187.         char buf[1000];
  188.         vsprintf (buf,ctl,list);
  189.         va_end (list);
  190.         xconf_error (MSG_U(E_MUSTBEROOT,"You must be root to\n%s"),buf);
  191.     }
  192.     return ret;
  193. }
  194.  
  195. /*
  196.     Set the html mode for granting priviledge.
  197.     In html mode we are executing as root all the time. Normally
  198.     no question is asked to root. Now we must ask for password before
  199.     going through.
  200. */
  201. void perm_sethtml (int _mode)
  202. {
  203.     perm_html_mode = _mode;
  204. }
  205.  
  206.